
package com.eworking.eim.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.zip.GZIPOutputStream;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.Context;

public class EasyUtil {
    
    private static Hashtable<String, String> resourceTable = new Hashtable<String, String>();
    
    public static boolean isAppRunningForeground(Context paramContext) {
        ActivityManager activityManager = (ActivityManager) paramContext.getSystemService("activity");
        List<RunningTaskInfo> lstTasks = activityManager.getRunningTasks(0);
        return paramContext.getPackageName().equalsIgnoreCase(lstTasks.get(0).baseActivity.getPackageName());
    }
    
    public static String getTopActivityName(Context paramContext) {
        ActivityManager activityManager = (ActivityManager) paramContext.getSystemService("activity");
        List<RunningTaskInfo> lstTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
        return lstTasks.get(0).topActivity.getClassName();
    }
    
    @SuppressLint("SimpleDateFormat")
    public static String getTimeStamp() {
        Date localDate = new Date(System.currentTimeMillis());
        return new SimpleDateFormat("yyyyMMddHHmmss").format(localDate);
    }
    
    public static boolean writeToZipFile(byte[] paramArrayOfByte, String paramString) {
        FileOutputStream localFileOutputStream = null;
        GZIPOutputStream localGZIPOutputStream = null;
        try {
            localFileOutputStream = new FileOutputStream(paramString);
            localGZIPOutputStream = new GZIPOutputStream(new BufferedOutputStream(localFileOutputStream));
            localGZIPOutputStream.write(paramArrayOfByte);
        } catch (Exception localException) {
            localException.printStackTrace();
            return false;
        } finally {
            if (localGZIPOutputStream != null)
                try {
                    localGZIPOutputStream.close();
                } catch (IOException localIOException3) {
                    localIOException3.printStackTrace();
                }
            if (localFileOutputStream != null)
                try {
                    localFileOutputStream.close();
                } catch (IOException localIOException4) {
                    localIOException4.printStackTrace();
                }
        }
        if (EIMLog.debugMode) {
            File localFile = new File(paramString);
            DecimalFormat localDecimalFormat = new DecimalFormat("#.##");
            double d1 = localFile.length() / paramArrayOfByte.length * 100.0D;
            double d2 = Double.valueOf(localDecimalFormat.format(d1)).doubleValue();
            EIMLog.d("zip", "data size:" + paramArrayOfByte.length + " zip file size:" + localFile.length()
                + "zip file ratio%: " + d2);
        }
        return true;
    }
    
    public static String getAppResourceString(Context paramContext, String paramString) {
        String str = (String) resourceTable.get(paramString);
        if (str != null)
            return str;
        int i = paramContext.getResources().getIdentifier(paramString, "string", paramContext.getPackageName());
        str = paramContext.getString(i);
        if (str != null)
            resourceTable.put(paramString, str);
        return str;
    }
    
    public static String convertByteArrayToString(byte[] paramArrayOfByte) {
        byte[] arrayOfByte;
        StringBuffer localStringBuffer = new StringBuffer();
        int j = (arrayOfByte = paramArrayOfByte).length;
        for (int i = 0; i < j; ++i) {
            byte b = arrayOfByte[i];
            localStringBuffer.append(String.format("0x%02X", new Object[] { Byte.valueOf(b) }));
        }
        return localStringBuffer.toString();
    }
}
